﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using XMLGener.Utils;

namespace XMLGener.Data
{
    /// <summary>
    /// 
    /// </summary>
    public class XMLOutputGenerator
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="document"></param>
        public XMLOutputGenerator(XMLGenerDocument document)
        {
            this.doc = document;
        }
        
      
        private bool fileIsOpened = false;
       
        private TextWriter outStream = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public bool openFile(int i)
        {
            if (fileIsOpened)
                return true;
            if (doc == null)
                return false;
            try
            {
                if (doc.outFile == null)
                {
                    doc.logger.addErrorLog(this.GetType().ToString(), "Output file is not defined.");
                    return false;
                }
                FileInfo fi = new FileInfo(doc.outFileFullPathIndex(i));
                doc.logger.addLog(this.GetType().ToString(), "Opening output file:" + fi.FullName);
                if (!fi.Directory.Exists)
                {
                    fi.Directory.Create();
                }
                if (!fi.Exists)
                {
                    this.outStream = new StreamWriter(fi.Create());
                }else
                    this.outStream = new StreamWriter(fi.FullName);
                if (this.outStream == null)
                {
                    doc.logger.addErrorLog(this.GetType().ToString(), "Can not open output file.");
                    return false;
                }

            }
            catch (Exception e)
            {
                doc.logger.addErrorLog(e.Source, e.Message);
                return false;
            }

            this.fileIsOpened = true;
            return true;
        }


        private XMLGenerDocument doc = null;
        private StringBuilder sb = new StringBuilder();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        public void write(string text)
        {
            this.write(text, 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        /// <param name="depth"></param>
        public void write(string text,int depth)
        {
            sb.Append(' ', depth*3);
            sb.Append(text);
            //this.outStream.Write(text);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        public void writeLine(string text)
        {
            writeLine(text,0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        /// <param name="depth"></param>
        public void writeLine(string text,int depth)
        {
            sb.Append(' ', depth * 3);
            sb.Append(text);
            if (!this.fileIsOpened)
            {
                if (this.doc != null)
                    this.doc.logger.addErrorLog(this.GetType().ToString(), "Error in writing text into file: File is not opened.");
                return;
            }
            this.outStream.WriteLine(sb.ToString());
            sb.Length = 0;
        }

        /// <summary>
        /// 
        /// </summary>
        public void close()
        {
            this.fileIsOpened = false;
            this.outStream.Close();
            sb.Length = 0;

        }



    }
}
